#!/usr/bin/env python
# coding: utf-8

# In[36]:


#动物识别，返回可能性最大的动物
#filename:图片名（本地存储包括路径）,animalnum展示识别结果的数量
def animal(filename,animalnum):
    request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/animal"
    
    # 二进制方式打开图片文件
    f = open(filename, 'rb')
    img = base64.b64encode(f.read())
    
    params = dict()
    params['image'] = img
    params['baike_num'] = animalnum
    params = urllib.parse.urlencode(params).encode("utf-8")
    #params = json.dumps(params).encode('utf-8')
    
    access_token = get_token()
    request_url = request_url + "?access_token=" + access_token
    request = urllib.request.Request(url=request_url, data=params)
    request.add_header('Content-Type', 'application/x-www-form-urlencoded')
    response = urllib.request.urlopen(request)
    content = response.read()
    if content:
        #print(content)
        content=content.decode('utf-8')
        #print(content)
        data = json.loads(content)
        result=data['result']
        
        nums=min(animalnum,len(result))
        
        for i in range(0,nums):
            item=result[i]
            print ('名称:',item['name'])
            print ('可能性:',item['score'])
        
    #return landmark
filename = 'C:\\Users\\Administrator\\Pictures\\API\\火烈鸟.jpg'
animal(filename,1)


# In[ ]:




